package com.sh.data.engine.application.service.integration;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sh.data.engine.application.model.dto.base.PagerResponse;
import com.sh.data.engine.application.model.dto.integration.*;
import com.sh.data.engine.application.model.dto.workspace.UserResponse;
import com.sh.data.engine.application.model.mapper.integration.OfflineSyncOperationTaskMonitorErrorResponsePageMapper;
import com.sh.data.engine.application.model.mapper.integration.OfflineSyncRunningRecordResponsePageMapper;
import com.sh.data.engine.common.util.ServletUtils;
import com.sh.data.engine.domain.base.model.PageResult;
import com.sh.data.engine.domain.integration.offline.model.domain.*;
import com.sh.data.engine.domain.integration.offline.service.OfflineSyncRunningRecordService;
import com.sh.data.engine.domain.integration.offline.service.OfflineSyncService;
import com.sh.data.engine.domain.workspace.manager.model.domain.UserDomain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * @author: mengzheng.mhc
 * @date: 2024/7/1 15:31
 */
@Slf4j
@Service
public class OfflineSyncOperationApplicationService {

    @Autowired
    private OfflineSyncRunningRecordService offlineSyncRunningRecordService;

    @Autowired
    private OfflineSyncService offlineSyncService;

    @Autowired
    private OfflineSyncRunningRecordResponsePageMapper offlineSyncRunningRecordResponsePageMapper;

    @Autowired
    private OfflineSyncOperationTaskMonitorErrorResponsePageMapper offlineSyncOperationTaskMonitorErrorResponsePageMapper;

    public OfflineSyncOperationSummaryResponse getOfflineSyncOperationSummary() {
        Long projectId = ServletUtils.getProjectId();
        OfflineSyncOperationSummaryDomain offlineSyncOperationSummaryDomain =
            offlineSyncRunningRecordService.getOfflineSyncSummary(projectId);

        return Convert.convert(
            OfflineSyncOperationSummaryResponse.class, offlineSyncOperationSummaryDomain);
    }

    public OfflineSyncOperationFinishedResponse getTodayFinished() {
        Long projectId = ServletUtils.getProjectId();
        OfflineSyncOperationTodayFinishedDomain offlineDevOperationTodayFinishedDomain =
            offlineSyncRunningRecordService.getTodayFinished(projectId);
        return Convert.convert(
            OfflineSyncOperationFinishedResponse.class, offlineDevOperationTodayFinishedDomain);
    }

    public List<OfflineSyncOperationFailedTopResponse> getFailedTaskTop(Integer timeType) {
        Long projectId = ServletUtils.getProjectId();
        List<OfflineSyncOperationFailedTopDomain> offlineSyncOperationFailedTopDomains =
            offlineSyncRunningRecordService.getFailedTaskTop(projectId, timeType);

        return Convert.toList(
            OfflineSyncOperationFailedTopResponse.class, offlineSyncOperationFailedTopDomains);
    }

    public List<OfflineSyncOperationRunningTopResponse> getRunningTimeTop(LocalDate runDate) {
        Long projectId = ServletUtils.getProjectId();
        List<OfflineSyncOperationRunningTimeTopDomain> offlineDevFailedTopDomain =
            offlineSyncRunningRecordService.getRunningTimeTop(projectId, runDate);

        return Convert.toList(OfflineSyncOperationRunningTopResponse.class, offlineDevFailedTopDomain);
    }

    public OfflineSyncOperationTaskMonitorResponse getTaskMonitorError() {
        Long projectId = ServletUtils.getProjectId();
        OfflineSyncOperationTaskMonitorDomain taskMonitorErrorDomain =
            offlineSyncRunningRecordService.getTaskMonitorError(projectId);
        return Convert.convert(OfflineSyncOperationTaskMonitorResponse.class, taskMonitorErrorDomain);
    }

    public PagerResponse<OfflineSyncOperationTaskMonitorErrorResponse> getTaskMonitorErrorTask(
        Integer timeCase, Integer pageNum, Integer pageSize
    ) {
        Long projectId = ServletUtils.getProjectId();
        IPage<OfflineSyncOperationTaskMonitorErrorTaskDomain> taskMonitorErrorDomain =
            offlineSyncRunningRecordService.getTaskMonitorErrorTask(
                projectId, timeCase, pageNum, pageSize);

        PageResult<OfflineSyncOperationTaskMonitorErrorResponse> r1 =
            PageResult.copyPropertiesAndConvert(
                taskMonitorErrorDomain, OfflineSyncOperationTaskMonitorErrorResponse.class);
        return offlineSyncOperationTaskMonitorErrorResponsePageMapper.toPageResponse(r1);
    }

    public PagerResponse<OfflineSyncRunningRecordResponse> historyTask(
        Integer pageNum, Integer pageSize, Long taskId, String taskStatus,
        String executeType, String creatorId, Date startTime, Date endTime,
        String userId, String order, String orderField, String nodeName, Integer ignored) {
        OfflineSyncTaskQueryDomain queryDomain =
            OfflineSyncTaskQueryDomain.builder()
                .taskId(taskId)
                .pageNum(pageNum)
                .pageSize(pageSize)
                .taskStatusList(taskStatus)
                .executeTypeList(executeType)
                .userId(creatorId)
                .startTime(startTime)
                .endTime(endTime)
                .userId(userId)
                .orderField(orderField)
                .order(order)
                .taskName(nodeName)
                .ignored(ignored)
                .build();
        PageResult<OfflineSyncRunningRecordDomain> pageDomain =
            offlineSyncService.historyTask(queryDomain);
        return offlineSyncRunningRecordResponsePageMapper.toPageResponse(pageDomain);
    }

    public boolean stopTasks(String logIds) {
        offlineSyncService.stop(logIds);
        return true;
    }

    public String run(Long taskId, Integer runType) {
        return offlineSyncService.run(taskId, runType);
    }

    public OfflineSyncLogResponse getOfflineSyncLog(Long logId, Integer offset) {
        OfflineSyncLogDomain offlineSyncLogDomain = offlineSyncService.getOfflineSyncLog(logId, offset);
        return Convert.convert(OfflineSyncLogResponse.class, offlineSyncLogDomain);
    }

    public List<UserResponse> getUserList() {
        Long projectId = ServletUtils.getProjectId();
        List<UserDomain> all = offlineSyncService.getUserIds(projectId);
        return Convert.toList(UserResponse.class, all);
    }

    public boolean ignoreTasks(String ids) {
        offlineSyncRunningRecordService.ignoreOfflineSyncRunning(ids);
        return true;
    }
}
